package com.module.base.base.adapter;

import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.MultiItemEntity;

/**
 * Description :
 *
 * @author bsnl_yanxuwen
 * @date 2019/12/1615:08
 * 多视图的适配器，
 * 替换SmoothRefreshLayout 加载功能，使用Adapter实现加载onCreateMultiViewHolder
 * 使用OnRefreshPageListener来监听
 */
public abstract class BaseMultiAdapter extends BaseAdapter {

    public abstract BaseViewHolder onCreateMultiViewHolder(ViewGroup parent, int viewType);

    @Override
    public BaseViewHolder onCreateDefaultViewHolder(ViewGroup parent, int viewType) {
        return onCreateMultiViewHolder(parent, viewType);
    }

    /**
     * 需要去除头部的 ItemViewType
     */
    @Override
    public final int getItemViewType(int position) {
        if (getHeaderLayoutCount() != 0 && getHeaderLayoutCount() > position) {
            return super.getItemViewType(position);
        }
        if (getData() != null && getData().size() + getHeaderLayoutCount() > position) {
            return getRealItemViewType(position - getHeaderLayoutCount());
        }
        return super.getItemViewType(position);
    }

    /**
     * 已去除头部的 ItemViewType
     */
    protected int getRealItemViewType(int position) {
        try {
            MultiItemEntity multiItemEntity = (MultiItemEntity) getData().get(position);
            return multiItemEntity.getItemType();
        } catch (Exception e) {
            return 0;
        }
    }
}
